{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Listing all Unums\n",
    "\n",
    "*The End of Error*, §4.10 \"A complete exact unum set for a small utag\" lists all the (2,2) unums. We can recreate this"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using Unums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "900-element Array{Unums.Unum{2,2,UInt16},1}:\n",
       "   0.0\n",
       "   1.0\n",
       "   2.0\n",
       "   3.0\n",
       "   0.0\n",
       "   0.5\n",
       "   1.0\n",
       "   1.5\n",
       "   2.0\n",
       "   2.5\n",
       "   3.0\n",
       "   3.5\n",
       "   0.0\n",
       "   ⋮  \n",
       " 320.0\n",
       " 336.0\n",
       " 352.0\n",
       " 368.0\n",
       " 384.0\n",
       " 400.0\n",
       " 416.0\n",
       " 432.0\n",
       " 448.0\n",
       " 464.0\n",
       " 480.0\n",
       " Inf  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a vector of all exact unums with positive sign bit\n",
    "E = Array(Unum22,0)\n",
    "for es in 1:2^2\n",
    "    for fs in 1:2^2\n",
    "        for e = 0:(2^es-1)\n",
    "            for f = 0:(2^fs-1)\n",
    "                u = Unum22(false,e,f,false,es,fs) # set s and u bits to false\n",
    "                push!(E,u)\n",
    "            end\n",
    "        end\n",
    "    end\n",
    "end\n",
    "E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "256-element Array{Any,1}:\n",
       "   0.0        \n",
       "   0.000976563\n",
       "   0.00195313 \n",
       "   0.00292969 \n",
       "   0.00390625 \n",
       "   0.00488281 \n",
       "   0.00585938 \n",
       "   0.00683594 \n",
       "   0.0078125  \n",
       "   0.00878906 \n",
       "   0.00976563 \n",
       "   0.0107422  \n",
       "   0.0117188  \n",
       "   ⋮          \n",
       " 320.0        \n",
       " 336.0        \n",
       " 352.0        \n",
       " 368.0        \n",
       " 384.0        \n",
       " 400.0        \n",
       " 416.0        \n",
       " 432.0        \n",
       " 448.0        \n",
       " 464.0        \n",
       " 480.0        \n",
       " Inf          "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# All exact 2,2 unums can be represented by a Float64\n",
    "F = [convert(Float64,x) for x in E]\n",
    "Fu = unique(F)\n",
    "sort!(Fu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "256-element Array{Any,1}:\n",
       "   0//1   \n",
       "   1//1024\n",
       "   1//512 \n",
       "   3//1024\n",
       "   1//256 \n",
       "   5//1024\n",
       "   3//512 \n",
       "   7//1024\n",
       "   1//128 \n",
       "   9//1024\n",
       "   5//512 \n",
       "  11//1024\n",
       "   3//256 \n",
       "    ⋮     \n",
       " 320//1   \n",
       " 336//1   \n",
       " 352//1   \n",
       " 368//1   \n",
       " 384//1   \n",
       " 400//1   \n",
       " 416//1   \n",
       " 432//1   \n",
       " 448//1   \n",
       " 464//1   \n",
       " 480//1   \n",
       "   1//0   "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R = [convert(Rational{Int},x) for x in E]\n",
    "Ru = unique(R)\n",
    "sort!(Ru)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# number of zeros\n",
    "sum(F .== 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.4.3",
   "language": "julia",
   "name": "julia-0.4"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
